<template>
  <a-modal
    :title='title'
    :width='1200'
    :visible='visible'
    :confirmLoading='confirmLoading'
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'>
    <a-spin :spinning='confirmLoading'>
      <a-form :form='form'>
        <a-row>
          <a-divider orientation='left' style='font-weight:bold'>基本信息</a-divider>
        </a-row>
        <a-row>
          <a-col :span='12'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='人员姓名'>
              <a-input placeholder='请输入人员姓名' :maxLength=100 v-decorator="['userName', validatorRules.userName ]" />
            </a-form-item>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='证件类型'>
              <a-select show-search placeholder='请输入证件类型'
                        v-decorator="['certType',{rules:[{required:true,message:'请选择证件类型'}]}]">
                <a-select-option v-for='item in certTypeList' :value='item.value' :key='item.value'>
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='证件号码'>
              <a-input placeholder='请输入证件号码' :maxLength=50
                       v-decorator="['idCard', {rules:[{required:true,message:'请输入证件号码'}]}]"  />
            </a-form-item>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='手机号'>
              <a-input placeholder='请输入手机号' v-decorator="['mobile', validatorRules.mobile]"  />
            </a-form-item>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='性别'>
              <a-radio-group v-decorator="['gender', {initialValue:1}]">
                <a-radio :value='1' checked> 男</a-radio>
                <a-radio :value='2'> 女</a-radio>
              </a-radio-group>
            </a-form-item>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='备注'>
              <a-textarea :rows='3' :maxLength=200 v-decorator="['remark', {}]" placeholder='请输入内容' allow-clear
                           />
            </a-form-item>
          </a-col>
          <a-col :span='12' style='height: 439px'>

            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='{xs: {span: 24},sm: {span: 24}}'
              label=''>
              <a-row type='flex' justify='space-between'>
                <a-radio-group v-model='uploadImgType' @change='changeUploadType'>
                  <a-radio-button value='0' style='width: 100px;text-align: center'>
                    上传
                  </a-radio-button>
                  <a-radio-button style='width: 100px;text-align: center' value='1'>
                    拍摄
                  </a-radio-button>
                </a-radio-group>
                <a-button style='width: 80px;' type='primary' v-show="uploadImgType==='1'" @click='setImage()'>
                  {{ imgSrc === '' ? '拍照' : '重拍' }}
                </a-button>
              </a-row>
              <div class='clearfix' v-show="uploadImgType==='0'">
                <a-upload
                  list-type='picture-card'
                  :file-list='fileList'
                  @preview='handlePreview'
                  @change='handleChange'
                  :before-upload='beforeUpload'
                >
                  <div v-if='fileList.length < 1'>
                    <a-icon type='plus' />
                    <div class='ant-upload-text'>
                      上传
                    </div>
                  </div>
                </a-upload>
                <a-modal :visible='previewVisible' :footer='null' @cancel='handleImageCancel'>
                  <img alt='example' style='width: 100%' :src='previewImage' />
                </a-modal>
              </div>
              <div v-show="uploadImgType==='1'">
                <video
                  v-show="imgSrc===''"
                  id='videoCamera'
                  :width='videoWidth'
                  :height='videoHeight'
                ></video>
                <canvas
                  v-show="imgSrc!==''"
                  id='canvasCamera'
                  :width='videoWidth'
                  :height='videoHeight'
                ></canvas>

              </div>

            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span='23'>
            <a-divider orientation='left' style='font-weight:bold'>详细信息</a-divider>
          </a-col>
          <a-col :span='1' style='vertical-align: center;height:100%'>
            <a @click='foldClick'>{{ foldText }}</a>
          </a-col>
        </a-row>
        <a-row v-show='showDetailInfo'>
          <a-col :span='12'>

            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='国籍'>
              <a-select v-decorator="['nation', {}]" >
                <a-select-option v-for='item in countryList' :key='item.value' :label='item.label' :value='item.value'>
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='籍贯'>
              <a-input placeholder='请输入籍贯' :maxLength=100 v-decorator="['hometown', {}]"  />
            </a-form-item>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='住址'>
              <a-input placeholder='请输入住址' :maxLength=200 v-decorator="['address', {}]" />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='民族'>
              <a-select v-decorator="['ethnic', {}]">
                <a-select-option v-for='item in nationList' :key='item.value' :label='item.label' :value='item.value'>
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='出生日期'
            >
              <a-date-picker v-decorator="['birthday', {}]" placeholder='出生日期'/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-divider orientation='left' style='font-weight:bold'>房屋信息</a-divider>
        </a-row>
        <a-row v-for='i in houseCount'
               style='border: 1px solid #e8e8e8;border-radius:10px;padding-top: 20px;margin-bottom:8px'>
          <a-col :span='12'>

            <a-form-item :labelCol='labelCol'
                         :wrapperCol='wrapperCol' label='项目'>
              <a-select :show-search='showSearch' placeholder='请输入项目' :filter-option='filterOption' label-in-value
                        style='width: 200px;margin-right: 20px'
                        v-decorator="['projectId',{rules:[{required:true,message:'请选择项目！'}]}]" @change='initAreaList'
                        :options='projectList'>
              </a-select>
            </a-form-item>

            <a-form-item :labelCol='labelCol'
                         :wrapperCol='wrapperCol' label='楼栋'>
              <a-select label-in-value :show-search='showSearch' :filter-option='filterOption' placeholder='可为空'
                        style='width: 300px;margin-right: 20px' v-decorator="['buildId',{}]" @change='initUnitList'
                        :options='buildList'>
              </a-select>
            </a-form-item>

            <a-form-item :labelCol='labelCol'
                         :wrapperCol='wrapperCol' label='楼层'>
              <a-select label-in-value :show-search='showSearch' :filter-option='filterOption' placeholder='可为空'
                        style='width: 300px;margin-right: 20px' v-decorator="['floorId',{}]" @change='initHouseList'
                        :options='floorList'>
              </a-select>
            </a-form-item>

            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='部门组织'>
              <a-select label-in-value :show-search='showSearch' style='width: 300px;margin-right: 20px'
                        :filter-option='filterOption'
                        placeholder='请选择部门组织' v-decorator="['departmentId', {}]" :options='departmentList'
                        >
              </a-select>
            </a-form-item>
          </a-col>


          <a-col :span='12'>

            <a-form-item :labelCol='labelCol'
                         :wrapperCol='wrapperCol' label='区域'>
              <a-select label-in-value :show-search='showSearch' placeholder='请输入区域' :filter-option='filterOption'
                        style='width: 200px;margin-right: 20px'
                        v-decorator="['areaId',{rules:[{required: true,message:'请选择区域'}]}]" @change='initBuildList'
                        :options='areaList'>
              </a-select>
            </a-form-item>

            <a-form-item :labelCol='labelCol'
                         :wrapperCol='wrapperCol' label='单元'>
              <a-select label-in-value :show-search='showSearch' :filter-option='filterOption' placeholder='可为空'
                        style='width: 300px;margin-right: 20px' v-decorator="['unitId',{}]" @change='initFloorList'
                        :options='unitList'>

              </a-select>
            </a-form-item>

            <a-form-item :labelCol='labelCol'
                         :wrapperCol='wrapperCol' label='房间号'>
              <a-select label-in-value :show-search='showSearch' :filter-option='filterOption' placeholder='可为空' style='width: 300px;margin-right: 20px' v-decorator="['houseId',{}]"
                        :options='houseList'>
              </a-select>
            </a-form-item>

            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='人员类型'>
              <a-select label-in-value placeholder='请选择人员类型' :filter-option='filterOption'
                        :options='projectUserTypeList' :show-search='showSearch'
                        v-decorator="['projectUserTypeId', {}]" />
            </a-form-item>
            <a-icon @click='minusHouseClick' type='minus-circle' :style="{ fontSize: '30px', color: '#1890ff' }" />
          </a-col>
        </a-row>
        <a-row style='text-align: center'>
          <a-button ghost type='primary' style='width:80%;margin-top:15px' @click='addHouseClick'>+继续添加房屋信息</a-button>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'
import countryList from '@/components/countryList'
import nationList from '@/components/nationList'
import { getProjectList } from '../../../../api/api'
import Login from '../../../system/user/Login'


export default {
  name: 'UserModal',
  components: {
    countryList,
    nationList
  },
  data() {
    return {
      showSearch: true,
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      videoWidth: 574,
      videoHeight: 360,
      imgSrc: '',
      hasTake: false,
      canvas: null,
      context: null,
      video: null,
      openVideo: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        userName: { rules: [{ required: true, message: '请输入姓名!' }] },
        birthday: {
          rules: [{
            // required: true,
            pattern: /^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))$/,
            message: '日期格式不正确!'
          }]
        },
        mobile: {
          rules: [{
            required: true,
            pattern: /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/,
            message: '手机号格式不正确'
          }]
        }
      },
      url: {
        add: '/user/user/add',
        edit: '/user/user/edit',
        getProjectList: '/user/user/getProjectList',
        getAreaListByProjectId: '/user/user/getAreaListByProjectId',
        getHouseListByRegionId: '/user/user/getHouseListByRegionId',
        getDepartmentByProjectId: '/user/user/getDepartmentByProjectId',
        getProjectUserTypeList: '/user/user/getProUserTypeListByProId',
        getAreaInfos: '/area/getAreaList',
        getBuildInfos: '/building/getBuildList',
        getUnitInfos: '/unit/getUnitList',
        getFloorInfos: 'floor/getFloorList',
        getHouseInfos: '/house/getHouseList',
        addHouse: 'house/addHouse',
        getHousetByFloorId: '/user/user/getHousetByFloorId'
      },

      record: {},
      uploadImgType: '0',
      previewVisible: false,
      previewImage: '',
      fileList: [],
      projectList: [],
      buildList: [],
      unitList: [],
      floorList: [],
      areaList: [],
      houseList: [],
      projectUserTypeList: [],
      departmentList: [],
      projectId: '',
      fileData: {},
      oldData: {},
      houseName: '',

      certTypeList: [{ label: '请选择', value: '' }, { label: '身份证', value: '身份证' }, { label: '护照', value: '护照' },
        { label: '港澳通行证', value: '港澳通行证' }, { label: '台湾通行证', value: '台湾通行证' }, { label: '军官证', value: '军官证' },
        { label: '户口本', value: '户口本' }, { label: '驾驶证', value: '驾驶证' }],
      houseCount: 1,
      showDetailInfo: false,
      foldText: '展开',
      countryList: [
        { englishValue: 'China', label: '中国', value: '中国' },
        { englishValue: 'Russia', label: '俄罗斯', value: '俄罗斯' },
        { englishValue: 'Angola', label: '安哥拉', value: '安哥拉' },
        { englishValue: 'Afghanistan', label: '阿富汗', value: '阿富汗' },
        { englishValue: 'Albania', label: '阿尔巴尼亚', value: '阿尔巴尼亚' },
        { englishValue: 'Algeria', label: '阿尔及利亚', value: '阿尔及利亚' },
        { englishValue: 'Andorra', label: '安道尔共和国', value: '安道尔共和国' },
        { englishValue: 'Anguilla', label: '安圭拉岛', value: '安圭拉岛' },
        { englishValue: 'Antigua and Barbuda', label: '安提瓜和巴布达', value: '安提瓜和巴布达' },
        { englishValue: 'Argentina', label: '阿根廷', value: '阿根廷' },
        { englishValue: 'Armenia', label: '亚美尼亚', value: '亚美尼亚' },
        { englishValue: 'Ascension', label: '阿森松', value: '阿森松' },
        { englishValue: 'Australia', label: '澳大利亚', value: '澳大利亚' },
        { englishValue: 'Austria', label: '奥地利', value: '奥地利' },
        { englishValue: 'Azerbaijan', label: '阿塞拜疆', value: '阿塞拜疆' },
        { englishValue: 'Bahamas', label: '巴哈马', value: '巴哈马' },
        { englishValue: 'Bahrain', label: '巴林', value: '巴林' },
        { englishValue: 'Bangladesh', label: '孟加拉国', value: '孟加拉国' },
        { englishValue: 'Barbados', label: '巴巴多斯', value: '巴巴多斯' },
        { englishValue: 'Belarus', label: '白俄罗斯', value: '白俄罗斯' },
        { englishValue: 'Belgium', label: '比利时', value: '比利时' },
        { englishValue: 'Belize', label: '伯利兹', value: '伯利兹' },
        { englishValue: 'Benin', label: '贝宁', value: '贝宁' },
        { englishValue: 'Bermuda Is', label: '百慕大群岛', value: '百慕大群岛' },
        { englishValue: 'Bolivia', label: '玻利维亚', value: '玻利维亚' },
        { englishValue: 'Botswana', label: '博茨瓦纳', value: '博茨瓦纳' },
        { englishValue: 'Brazil', label: '巴西', value: '巴西' },
        { englishValue: 'Brunei', label: '文莱', value: '文莱' },
        { englishValue: 'Bulgaria', label: '保加利亚', value: '保加利亚' },
        { englishValue: 'Burkina Faso', label: '布基纳法索', value: '布基纳法索' },
        { englishValue: 'Burma', label: '缅甸', value: '缅甸' },
        { englishValue: 'Burundi', label: '布隆迪', value: '布隆迪' },
        { englishValue: 'Cameroon', label: '喀麦隆', value: '喀麦隆' },
        { englishValue: 'Canada', label: '加拿大', value: '加拿大' },
        { englishValue: 'Cayman Is', label: '开曼群岛', value: '开曼群岛' },
        { englishValue: 'Central African Republic', label: '中非共和国', value: '中非共和国' },
        { englishValue: 'Chad', label: '乍得', value: '乍得' },
        { englishValue: 'Chile', label: '智利', value: '智利' },

        { englishValue: 'Colombia', label: '哥伦比亚', value: '哥伦比亚' },
        { englishValue: 'Congo', label: '刚果', value: '刚果' },
        { englishValue: 'Cook Is', label: '库克群岛', value: '库克群岛' },
        { englishValue: 'Costa Rica', label: '哥斯达黎加', value: '哥斯达黎加' },
        { englishValue: 'Cuba', label: '古巴', value: '古巴' },
        { englishValue: 'Cyprus', label: '塞浦路斯', value: '塞浦路斯' },
        { englishValue: 'Czech Republic', label: '捷克', value: '捷克' },
        { englishValue: 'Denmark', label: '丹麦', value: '丹麦' },
        { englishValue: 'Djibouti', label: '吉布提', value: '吉布提' },
        { englishValue: 'Dominica Rep', label: '多米尼加共和国', value: '多米尼加共和国' },
        { englishValue: 'Ecuador', label: '厄瓜多尔', value: '厄瓜多尔' },
        { englishValue: 'Egypt', label: '埃及', value: '埃及' },
        { englishValue: 'EI Salvador', label: '萨尔瓦多', value: '萨尔瓦多' },
        { englishValue: 'Estonia', label: '爱沙尼亚', value: '爱沙尼亚' },
        { englishValue: 'Ethiopia', label: '埃塞俄比亚', value: '埃塞俄比亚' },
        { englishValue: 'Fiji', label: '斐济', value: '斐济' },
        { englishValue: 'Finland', label: '芬兰', value: '芬兰' },
        { englishValue: 'France', label: '法国', value: '法国' },
        { englishValue: 'French Guiana', label: '法属圭亚那', value: '法属圭亚那' },
        { englishValue: 'French Polynesia', label: '法属玻利尼西亚', value: '法属玻利尼西亚' },
        { englishValue: 'Gabon', label: '加蓬', value: '加蓬' },
        { englishValue: 'Gambia', label: '冈比亚', value: '冈比亚' },
        { englishValue: 'Georgia', label: '格鲁吉亚', value: '格鲁吉亚' },
        { englishValue: 'Germany', label: '德国', value: '德国' },
        { englishValue: 'Ghana', label: '加纳', value: '加纳' },
        { englishValue: 'Gibraltar', label: '直布罗陀', value: '直布罗陀' },
        { englishValue: 'Greece', label: '希腊', value: '希腊' },
        { englishValue: 'Grenada', label: '格林纳达', value: '格林纳达' },
        { englishValue: 'Guam', label: '关岛', value: '关岛' },
        { englishValue: 'Guatemala', label: '危地马拉', value: '危地马拉' },
        { englishValue: 'Guinea', label: '几内亚', value: '几内亚' },
        { englishValue: 'Guyana', label: '圭亚那', value: '圭亚那' },
        { englishValue: 'Haiti', label: '海地', value: '海地' },
        { englishValue: 'Honduras', label: '洪都拉斯', value: '洪都拉斯' },
        { englishValue: 'Hungary', label: '匈牙利', value: '匈牙利' },
        { englishValue: 'Iceland', label: '冰岛', value: '冰岛' },
        { englishValue: 'India', label: '印度', value: '印度' },
        { englishValue: 'Indonesia', label: '印度尼西亚', value: '印度尼西亚' },
        { englishValue: 'Iran', label: '伊朗', value: '伊朗' },
        { englishValue: 'Iraq', label: '伊拉克', value: '伊拉克' },
        { englishValue: 'Ireland', label: '爱尔兰', value: '爱尔兰' },
        { englishValue: 'Israel', label: '以色列', value: '以色列' },
        { englishValue: 'Italy', label: '意大利', value: '意大利' },
        { englishValue: 'Ivory Coast', label: '科特迪瓦', value: '科特迪瓦' },
        { englishValue: 'Jamaica', label: '牙买加', value: '牙买加' },
        { englishValue: 'Japan', label: '日本', value: '日本' },
        { englishValue: 'Jordan', label: '约旦', value: '约旦' },
        { englishValue: 'Kampuchea (Cambodia)', label: '柬埔寨', value: '柬埔寨' },
        { englishValue: 'Kazakstan', label: '哈萨克斯坦', value: '哈萨克斯坦' },
        { englishValue: 'Kenya', label: '肯尼亚', value: '肯尼亚' },
        { englishValue: 'Korea', label: '韩国', value: '韩国' },
        { englishValue: 'Kuwait', label: '科威特', value: '科威特' },
        { englishValue: 'Kyrgyzstan', label: '吉尔吉斯坦', value: '吉尔吉斯坦' },
        { englishValue: 'Laos', label: '老挝', value: '老挝' },
        { englishValue: 'Latvia', label: '拉脱维亚', value: '拉脱维亚' },
        { englishValue: 'Lebanon', label: '黎巴嫩', value: '黎巴嫩' },
        { englishValue: 'Lesotho', label: '莱索托', value: '莱索托' },
        { englishValue: 'Liberia', label: '利比里亚', value: '利比里亚' },
        { englishValue: 'Libya', label: '利比亚', value: '利比亚' },
        { englishValue: 'Liechtenstein', label: '列支敦士登', value: '列支敦士登' },
        { englishValue: 'Lithuania', label: '立陶宛', value: '立陶宛' },
        { englishValue: 'Luxembourg', label: '卢森堡', value: '卢森堡' },
        { englishValue: 'Madagascar', label: '马达加斯加', value: '马达加斯加' },
        { englishValue: 'Malawi', label: '马拉维', value: '马拉维' },
        { englishValue: 'Malaysia', label: '马来西亚', value: '马来西亚' },
        { englishValue: 'Maldives', label: '马尔代夫', value: '马尔代夫' },
        { englishValue: 'Mali', label: '马里', value: '马里' },
        { englishValue: 'Malta', label: '马耳他', value: '马耳他' },
        { englishValue: 'Mariana Is', label: '马里亚那群岛', value: '马里亚那群岛' },
        { englishValue: 'Martinique', label: '马提尼克', value: '马提尼克' },
        { englishValue: 'Mauritius', label: '毛里求斯', value: '毛里求斯' },
        { englishValue: 'Mexico', label: '墨西哥', value: '墨西哥' },
        { englishValue: 'Moldova', label: '摩尔多瓦', value: '摩尔多瓦' },
        { englishValue: 'Monaco', label: '摩纳哥', value: '摩纳哥' },
        { englishValue: 'Mongolia', label: '蒙古', value: '蒙古' },
        { englishValue: 'Montserrat Is', label: '蒙特塞拉特岛', value: '蒙特塞拉特岛' },
        { englishValue: 'Morocco', label: '摩洛哥', value: '摩洛哥' },
        { englishValue: 'Mozambique', label: '莫桑比克', value: '莫桑比克' },
        { englishValue: 'Namibia', label: '纳米比亚', value: '纳米比亚' },
        { englishValue: 'Nauru', label: '瑙鲁', value: '瑙鲁' },
        { englishValue: 'Nepal', label: '尼泊尔', value: '尼泊尔' },
        { englishValue: 'Netheriands Antilles', label: '荷属安的列斯', value: '荷属安的列斯' },
        { englishValue: 'Netherlands', label: '荷兰', value: '荷兰' },
        { englishValue: 'New Zealand', label: '新西兰', value: '新西兰' },
        { englishValue: 'Nicaragua', label: '尼加拉瓜', value: '尼加拉瓜' },
        { englishValue: 'Niger', label: '尼日尔', value: '尼日尔' },
        { englishValue: 'Nigeria', label: '尼日利亚', value: '尼日利亚' },
        { englishValue: 'North Korea', label: '朝鲜', value: '朝鲜' },
        { englishValue: 'Norway', label: '挪威', value: '挪威' },
        { englishValue: 'Oman', label: '阿曼', value: '阿曼' },
        { englishValue: 'Pakistan', label: '巴基斯坦', value: '巴基斯坦' },
        { englishValue: 'Panama', label: '巴拿马', value: '巴拿马' },
        { englishValue: 'Papua New Cuinea', label: '巴布亚新几内亚', value: '巴布亚新几内亚' },
        { englishValue: 'Paraguay', label: '巴拉圭', value: '巴拉圭' },
        { englishValue: 'Peru', label: '秘鲁', value: '秘鲁' },
        { englishValue: 'Philippines', label: '菲律宾', value: '菲律宾' },
        { englishValue: 'Poland', label: '波兰', value: '波兰' },
        { englishValue: 'Portugal', label: '葡萄牙', value: '葡萄牙' },
        { englishValue: 'Puerto Rico', label: '波多黎各', value: '波多黎各' },
        { englishValue: 'Qatar', label: '卡塔尔', value: '卡塔尔' },
        { englishValue: 'Reunion', label: '留尼旺', value: '留尼旺' },
        { englishValue: 'Romania', label: '罗马尼亚', value: '罗马尼亚' },

        // {englishValue: 'Saint Lueia', label: '圣卢西亚', value: '圣卢西亚'},
        { englishValue: 'Saint Vincent', label: '圣文森特岛', value: '圣文森特岛' },
        { englishValue: 'Samoa Eastern', label: '东萨摩亚(美)', value: '东萨摩亚(美)' },
        { englishValue: 'Samoa Western', label: '西萨摩亚', value: '西萨摩亚' },
        { englishValue: 'San Marino', label: '圣马力诺', value: '圣马力诺' },
        { englishValue: 'Sao Tome and Principe', label: '圣多美和普林西比', value: '圣多美和普林西比' },
        { englishValue: 'Saudi Arabia', label: '沙特阿拉伯', value: '沙特阿拉伯' },
        { englishValue: 'Senegal', label: '塞内加尔', value: '塞内加尔' },
        { englishValue: 'Seychelles', label: '塞舌尔', value: '塞舌尔' },
        { englishValue: 'Sierra Leone', label: '塞拉利昂', value: '塞拉利昂' },
        { englishValue: 'Singapore', label: '新加坡', value: '新加坡' },
        { englishValue: 'Slovakia', label: '斯洛伐克', value: '斯洛伐克' },
        { englishValue: 'Slovenia', label: '斯洛文尼亚', value: '斯洛文尼亚' },
        { englishValue: 'Solomon Is', label: '所罗门群岛', value: '所罗门群岛' },
        { englishValue: 'Somali', label: '索马里', value: '索马里' },
        { englishValue: 'South Africa', label: '南非', value: '南非' },
        { englishValue: 'Spain', label: '西班牙', value: '西班牙' },
        { englishValue: 'SriLanka', label: '斯里兰卡', value: '斯里兰卡' },
        { englishValue: 'St.Lucia', label: '圣卢西亚', value: '圣卢西亚' },
        { englishValue: 'St.Vincent', label: '圣文森特', value: '圣文森特' },
        { englishValue: 'Sudan', label: '苏丹', value: '苏丹' },
        { englishValue: 'Suriname', label: '苏里南', value: '苏里南' },
        { englishValue: 'Swaziland', label: '斯威士兰', value: '斯威士兰' },
        { englishValue: 'Sweden', label: '瑞典', value: '瑞典' },
        { englishValue: 'Switzerland', label: '瑞士', value: '瑞士' },
        { englishValue: 'Syria', label: '叙利亚', value: '叙利亚' },
        { englishValue: 'Tajikstan', label: '塔吉克斯坦', value: '塔吉克斯坦' },
        { englishValue: 'Tanzania', label: '坦桑尼亚', value: '坦桑尼亚' },
        { englishValue: 'Thailand', label: '泰国', value: '泰国' },
        { englishValue: 'Togo', label: '多哥', value: '多哥' },
        { englishValue: 'Tonga', label: '汤加', value: '汤加' },
        { englishValue: 'Trinidad and Tobago', label: '特立尼达和多巴哥', value: '特立尼达和多巴哥' },
        { englishValue: 'Tunisia', label: '突尼斯', value: '突尼斯' },
        { englishValue: 'Turkey', label: '土耳其', value: '土耳其' },
        { englishValue: 'Turkmenistan', label: '土库曼斯坦', value: '土库曼斯坦' },
        { englishValue: 'Uganda', label: '乌干达', value: '乌干达' },
        { englishValue: 'Ukraine', label: '乌克兰', value: '乌克兰' },
        { englishValue: 'United Arab Emirates', label: '阿拉伯联合酋长国', value: '阿拉伯联合酋长国' },
        { englishValue: 'United Kiongdom', label: '英国', value: '英国' },
        { englishValue: 'United States of America', label: '美国', value: '美国' },
        { englishValue: 'Uruguay', label: '乌拉圭', value: '乌拉圭' },
        { englishValue: 'Uzbekistan', label: '乌兹别克斯坦', value: '乌兹别克斯坦' },
        { englishValue: 'Venezuela', label: '委内瑞拉', value: '委内瑞拉' },
        { englishValue: 'Vietnam', label: '越南', value: '越南' },
        { englishValue: 'Yemen', label: '也门', value: '也门' },
        { englishValue: 'Yugoslavia', label: '南斯拉夫', value: '南斯拉夫' },
        { englishValue: 'Zimbabwe', label: '津巴布韦', value: '津巴布韦' },
        { englishValue: 'Zaire', label: '扎伊尔', value: '扎伊尔' },
        { englishValue: 'Zambia', label: '赞比亚', value: '赞比亚}' }
      ],
      nationList:
        [
          { label: '汉族', value: '汉族' },
          { label: '蒙古族', value: '蒙古族' },
          { label: '回族', value: '回族' },
          { label: '藏族', value: '藏族' },
          { label: '维吾尔族', value: '维吾尔族' },
          { label: '苗族', value: '苗族' },
          { label: '彝族', value: '彝族' },
          { label: '壮族', value: '壮族' },
          { label: '布依族', value: '布依族' },
          { label: '朝鲜族', value: '朝鲜族' },
          { label: '满族', value: '满族' },
          { label: '侗族', value: '侗族' },
          { label: '瑶族', value: '瑶族' },
          { label: '白族', value: '白族' },
          { label: '土家族', value: '土家族' },
          { label: '哈尼族', value: '哈尼族' },
          { label: '哈萨克族', value: '哈萨克族' },
          { label: '傣族', value: '傣族' },
          { label: '黎族', value: '黎族' },
          { label: '傈僳族', value: '傈僳族' },
          { label: '佤族', value: '佤族' },
          { label: '畲族', value: '畲族' },
          { label: '高山族', value: '高山族' },
          { label: '拉祜族', value: '拉祜族' },
          { label: '水族', value: '水族' },
          { label: '东乡族', value: '东乡族' },
          { label: '纳西族', value: '纳西族' },
          { label: '景颇族', value: '景颇族' },
          { label: '柯尔克孜族', value: '柯尔克孜族' },
          { label: '土族', value: '土族' },
          { label: '达斡尔族', value: '达斡尔族' },
          { label: '仫佬族', value: '仫佬族' },
          { label: '羌族', value: '羌族' },
          { label: '布朗族', value: '布朗族' },
          { label: '撒拉族', value: '撒拉族' },
          { label: '毛南族', value: '毛南族' },
          { label: '仡佬族', value: '仡佬族' },
          { label: '锡伯族', value: '锡伯族' },
          { label: '阿昌族', value: '阿昌族' },
          { label: '普米族', value: '普米族' },
          { label: '塔吉克族', value: '塔吉克族' },
          { label: '怒族', value: '怒族' },
          { label: '乌孜别克族', value: '乌孜别克族' },
          { label: '俄罗斯族', value: '俄罗斯族' },
          { label: '鄂温克族', value: '鄂温克族' },
          { label: '德昂族', value: '德昂族' },
          { label: '保安族', value: '保安族' },
          { label: '裕固族', value: '裕固族' },
          { label: '京族', value: '京族' },
          { label: '塔塔尔族', value: '塔塔尔族' },
          { label: '独龙族', value: '独龙族' },
          { label: '鄂伦春族', value: '鄂伦春族' },
          { label: '赫哲族', value: '赫哲族' },
          { label: '门巴族', value: '门巴族' },
          { label: '珞巴族', value: '珞巴族' },
          { label: '基诺族', value: '基诺族' }]
    }
  },


  created() {

  },
  methods: {
    filterOption(input, option) {
      let com = option.componentOptions
      let name = String(com.propsData.label)
      if (name.toLowerCase().includes(input.toLowerCase())) {
        return true
      }
    },
    projectInit() {
      getProjectList().then((res) => {
        this.form.resetFields(['areaName', 'buildName', 'unitName', 'floor'])
        if (res.success) {
          this.projectList = res.result
        }
      })
    },

    minusHouseClick() {
      this.houseCount = this.houseCount - 1
    },
    addHouseClick() {
      this.houseCount = this.houseCount + 1
    },
    foldClick() {
      this.showDetailInfo = !this.showDetailInfo
      if (this.foldText === '折叠') {
        this.foldText = '展开'
      } else {
        this.foldText = '折叠'
      }
    },


    countryChange(nation) {

      this.form.setFieldsValue({ nation: nation })
    },


    setFloorId(e) {
      this.floorId = e

    },

    //查询项目下的人员类型
    async getProjectUserTypeList(projectId) {
      const that = this
      await getAction(this.url.getProjectUserTypeList, { projectId: projectId }).then((res) => {
        if (res.success) {
          if (res.result != null) {
            res.result.forEach(item => {
              that.projectUserTypeList.push({
                value: item.id,
                key: item.id,
                label: item.typeName
              })
            })
          }
        }
      })
    },
    //查询项目下的部门组织
    async getDepartmentByProjectId(projectId) {
      await getAction(this.url.getDepartmentByProjectId, { projectId: projectId }).then((res) => {
        if (res.success) {
          if (res.result != null) {
            res.result.forEach((item) => {
              this.departmentList.push({
                value: item.id,
                key: item.id,
                label: item.name
              })
            })
          }

        }
      })
    },

    //查询所有项目
    getProjectList() {
      getAction(this.url.getProjectList).then((res) => {
        if (res.success) {
          this.projectList = res.result
        }
      })
    },

    handleImageCancel() {
      this.previewVisible = false
    },
    beforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isLt2M) {
        this.$message.error('图片大小不能超过10MB!')
        return false
      }
      const that = this
      this.getBase64(file, (imageUrl) => {
        that.fileData.set('imgData', imageUrl)
      })


      return false
    },
    getBase64(file, callback) {
      return new Promise((resolve, reject) => {
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => {
          callback(reader.result)
          resolve('成功')
        }
        reader.onerror = error => reject(error)
      })
    },

    async handlePreview(file) {
      if (!file.url && !file.preview) {
        file.preview = await this.getBase64(file.originFileObj)
      }
      this.previewImage = file.url || file.preview
      this.previewVisible = true
    },

    handleChange({ fileList }) {

      this.fileList = fileList
    },
    add() {

      this.edit({})
    },
    edit(record) {
      this.oldData = {}
      this.uploadImgType = '0'
      this.imgSrc = ''
      this.record = record
      this.projectList = []
      this.buildList = []
      this.unitList = []
      this.floorList = []
      this.areaList = []
      this.houseList = []
      this.projectUserTypeList = []
      this.departmentList = []
      this.projectUserTypeList.push({
        label:'请输入人员类型',
        value:''
      })
      if (record) {
        this.oldData = record
        this.model = Object.assign({}, record)
        this.initProjectList(record)

        if (record.imgUrl) {
          this.fileList.push({ name: 'face', uid: '-1', url: record.imgUrl })
        }
      }
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model,  'userName', 'mobile', 'certType', 'gender', 'remark', 'nation', 'ethnic', 'hometown', 'address', 'idCard'))
        if(record.birthday=="[]"){
         this.form.setFieldsValue({birthday:""})
        }
      })
      this.fileData = new FormData()
      this.form.resetFields()

    },
    close() {
      if (String(this.uploadImgType) === '1') {
        this.video.srcObject.getTracks()[0].stop();
      }
      this.$emit('close')
      this.visible = false
      this.fileList = []
    },
    async initProjectList(record) {
      if (record.id !== undefined && record.id !== null) {
        this.model = Object.assign({}, record)
        await this.getAreaList({ projectId: this.model.projectId })
        await this.getBuildList({ areaId: this.model.areaId })
        await this.getUnitList({ buildingId: this.model.buildingId })
        await this.getFloorList({ unitId: this.model.unitId })
        await this.getHouseList({ floorId: this.model.floorId })

        await this.getProjectUserTypeList(this.model.projectId)
        await this.getDepartmentByProjectId(this.model.projectId)
        this.$nextTick(() => {
          this.form.setFieldsValue({ projectId: { value: this.model.projectId, label: this.model.projectName } })
          this.form.setFieldsValue({ areaId: { value: this.model.areaId, label: this.model.areaName } })
          let build = this.buildList.find(item => {
            if (item.key === this.model.buildingId) {
              return true
            }
          })
          this.form.setFieldsValue({ buildId: build })
          let unit = this.unitList.find(item => {
            if (item.key === this.model.unitId) {
              return true
            }
          })
          this.form.setFieldsValue({ unitId: unit })
          let floor = this.floorList.find(item => {
            if (item.key === this.model.floorId) {
              return true
            }
          })
          this.form.setFieldsValue({ floorId: floor })
          let house = this.houseList.find(item => {
            if (item.key === this.model.houseId) {
              return true
            }
          })
          this.form.setFieldsValue({ houseId: house })
          let projectUserType = this.projectUserTypeList.find(item => {
            if (item.key == this.model.projectUserTypeId) {
              return true
            }
          })

          this.form.setFieldsValue({ projectUserTypeId: projectUserType })

          let department = this.departmentList.find(item => {
            if (item.key == this.model.departmentId) {
              return true
            }
          })
          this.form.setFieldsValue({ departmentId: department })
        })
      }
      getProjectList().then((res) => {
        this.projectList = []
        if (res.success) {
          if (res.result !== null) {
            res.result.forEach(item => {
              this.projectList.push({
                value: item.id,
                key: item.id,
                label: item.name
              })
            })
          }
        }
      })
    },
    initAreaList(e) {
      this.form.resetFields(['areaId', 'buildId', 'unitId', 'floorId', 'houseId', 'departmentId', 'projectUserTypeId'])
      this.projectUserTypeList = []
      this.departmentList = []
      this.getProjectUserTypeList(e.key)
      this.getDepartmentByProjectId(e.key)
      let params = {
        projectId: e.key
      }
      this.getAreaList(params)
    },
    async getAreaList(params) {
      this.loading = true
      await getAction(this.url.getAreaInfos, params).then((res) => {
        this.areaList = []
        if (res.success) {
          if (res.result !== null) {
            res.result.forEach(item => {
              this.areaList.push({
                value: item.id,
                key: item.id,
                label: item.name
              })
            })
          }

        }
        this.loading = false
      })
    },
    initBuildList(e) {
      this.form.resetFields(['buildId', 'unitId', 'floorId', 'houseId'])
      let params = {
        areaId: e.key
      }
      this.getBuildList(params)

    },
    async getBuildList(params) {
      this.loading = true
      await getAction(this.url.getBuildInfos, params).then((res) => {
        this.buildList = [{ value: '', label: '请选择楼栋' }]
        if (res.success) {
          if (res.result !== null) {
            res.result.forEach(item => {
              this.buildList.push({
                value: item.id,
                key: item.id,
                label: item.buildingName
              })
            })
          }
        }
        this.loading = false
      })
    },
    initUnitList(e) {
      this.form.resetFields(['unitId', 'floorId', 'houseId'])
      let params = {
        buildingId: e.key
      }
      this.getUnitList(params)
    },
    async getUnitList(params) {
      if (params.buildingId != null) {
        this.loading = true
        await getAction(this.url.getUnitInfos, params).then((res) => {
          this.unitList = [{ value: '', label: '请选择单元' }]
          if (res.success) {
            if (res.result !== null) {
              res.result.forEach(item => {
                this.unitList.push({
                  value: item.id,
                  key: item.id,
                  label: item.unitName
                })
              })
            }
          }
          this.loading = false
        })
      }
    },
    initFloorList(e) {
      this.form.resetFields(['floorId', 'houseId'])
      let params = {
        unitId: e.key
      }
      this.getFloorList(params)

    },
    async getFloorList(params) {
      if (params.unitId != null) {
        this.loading = true
        await getAction(this.url.getFloorInfos, params).then((res) => {
          this.floorList = [{ value: '', label: '请选择楼层' }]
          if (res.success) {
            if (res.result !== null) {
              res.result.forEach(item => {
                this.floorList.push({
                  value: item.id,
                  key: item.id,
                  label: item.floorName
                })
              })
            }
          }
          this.loading = false
        })
      }
    },
    initHouseList(e) {
      this.changefloor = '楼层：' + this.record.floorName + '->' + e.label
      this.form.resetFields(['houseId'])
      let params = {
        floorId: e.key
      }
      this.getHouseList(params)
    },
    async getHouseList(params) {
      if (params.floorId != null) {
        this.loading = true
        await getAction(this.url.getHouseInfos, params).then((res) => {
          this.houseList = [{ value: '', label: '请选择房间号' }]
          if (res.success) {
            if (res.result !== null) {

              res.result.forEach(item => {
                this.houseList.push({
                  value: item.id,
                  key: item.id,
                  label: item.houseName
                })
              })
            }
          }
          this.loading = false
        })
      }
    },
    changeUploadType() {
      if (String(this.uploadImgType) === '1') {
        if (this.imgSrc === '') {
          this.getCompetence()
        }
      } else {
        this.video.srcObject.getTracks()[0].stop()
      }
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          let method = ''
          let params = {}
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'

          } else {
            httpurl += this.url.edit
            params.id = this.model.id
            method = 'post'
          }
          this.model = {}
          let formData = Object.assign(this.model, values)
          let newData = {}
          //时间格式化
          formData.updateTime = formData.updateTime ? formData.updateTime.format('YYYY-MM-DD HH:mm:ss') : null
          params.userName = formData.userName
          params.mobile = formData.mobile
          params.certType = formData.certType
          params.idCard = formData.idCard
          params.gender = formData.gender
          params.imgData = formData.imgData
          if ((this.fileData.get('imgData') === undefined || this.fileData.get('imgData') === null) && this.fileList.length == 0) {
            this.$message.error('图片未上传')
            this.loading = false
            return
          }
          params.remark = formData.remark
          params.nation = formData.nation
          params.ethnic = formData.ethnic
          params.hometown = formData.hometown
          params.birthday = formData.birthday
          params.address = formData.address
          params.projectId = formData.projectId.key
          params.areaId = formData.areaId.key
          if (formData.buildId != undefined) {
            params.buildingId = formData.buildId.key
            newData.buildingName = formData.buildId.label
          }
          if (formData.unitId != undefined) {
            params.unitId = formData.unitId.key
            newData.unitName = formData.unitId.label
          }
          if (formData.floorId != undefined) {
            params.floorId = formData.floorId.key
            newData.floorName = formData.floorId.label
          }
          if (formData.houseId != undefined) {
            params.houseId = formData.houseId.key
            newData.houseName = formData.houseId.label
          }
          if (formData.departmentId != undefined) {
            params.departmentId = formData.departmentId.key
            newData.departmentName = formData.departmentId.label
          }
          if (formData.projectUserTypeId != undefined) {
            params.projectUserTypeId = formData.projectUserTypeId.key
            newData.projectUserTypeName = formData.projectUserTypeId.label
          }
          console.log("@@@@@@@@@@",formData)
          if (formData.gender===1){
            newData.sex= '男'
          }else if (formData.gender===2){
            newData.sex= '女'
          }
          if (this.oldData.gender===1){
            this.oldData.sex='男'
          }else if (this.oldData.gender===2){
            this.oldData.sex='女'
          }
          newData.userName = formData.userName
          newData.address = formData.address
          newData.birthday = moment(formData.birthday).format('yyyy-MM-DD HH:mm:ss')
          newData.idCard = formData.idCard
          newData.hometown = formData.hometown
          newData.ethnic = formData.ethnic
          newData.mobile = formData.mobile
          newData.nation = formData.nation
          newData.remark = formData.remark
          if (formData.projectId !== undefined) {
            newData.projectName = formData.projectId.label
          }
          if (formData.areaId !== undefined) {
            newData.areaName = formData.areaId.label
          }
          let diffStr = ''
          for (let newDataKey in newData) {
            /*判断新旧字段如果不相等*/
            if (newData[newDataKey] != this.oldData[newDataKey]) {
              if(newData[newDataKey]===undefined||newData[newDataKey]===null){
                newData[newDataKey]="[]"
              }
              if(this.oldData[newDataKey]===undefined||this.oldData[newDataKey]===null){
                this.oldData[newDataKey]="[]"
              }
              diffStr += newDataKey + ':' +this.oldData[newDataKey] + ' =>' + newData[newDataKey] +";"
            }
          }
          that.fileData.append('formData', JSON.stringify(params))
          that.fileData.append('diffStr', diffStr)
          that.confirmLoading = true
          httpAction(httpurl, that.fileData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    // 调用权限（打开摄像头功能）
    getCompetence() {
      var _this = this
      _this.cancas = document.getElementById('canvasCamera')
      _this.context = this.cancas.getContext('2d')
      _this.video = document.getElementById('videoCamera')
      _this.video.style.display = 'block'
      // 获取媒体属性，旧版本浏览器可能不支持mediaDevices，我们首先设置一个空对象
      if (navigator.mediaDevices === undefined) {
        navigator.mediaDevices = {}
      }
      // 一些浏览器实现了部分mediaDevices，我们不能只分配一个对象
      // 使用getUserMedia，因为它会覆盖现有的属性。
      // 这里，如果缺少getUserMedia属性，就添加它。
      if (navigator.mediaDevices.getUserMedia === undefined) {
        navigator.mediaDevices.getUserMedia = function(constraints) {
          // 首先获取现存的getUserMedia(如果存在)
          var getUserMedia =
            navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia ||
            navigator.getUserMedia
          // 有些浏览器不支持，会返回错误信息
          // 保持接口一致
          if (!getUserMedia) {
            //不存在则报错
            return Promise.reject(
              new Error('getUserMedia is not implemented in this browser')
            )
          }
          // 否则，使用Promise将调用包装到旧的navigator.getUserMedia
          return new Promise(function(resolve, reject) {
            getUserMedia.call(navigator, constraints, resolve, reject)
          })
        }
      }
      var constraints = {
        audio: false,
        video: {
          width: this.videoWidth,
          height: this.videoHeight,
          transform: 'scaleX(-1)'
        }
      }
      navigator.mediaDevices
        .getUserMedia(constraints)
        .then(function(stream) {
          // 旧的浏览器可能没有srcObject
          if ('srcObject' in _this.video) {
            _this.video.srcObject = stream
          } else {
            // 避免在新的浏览器中使用它，因为它正在被弃用。
            _this.video.src = window.URL.createObjectURL(stream)
          }
          _this.video.onloadedmetadata = function(e) {
            _this.video.play()
          }
        })
        .catch((err) => {
          alert('摄像头打开失败')
          console.log(err)
        })
    },
    //  绘制图片（拍照功能）
    setImage() {
      if (this.imgSrc !== '') {
        this.imgSrc = ''
        return
      }
      let _this = this
      // canvas画图
      _this.context.drawImage(
        _this.video,
        0,
        0,
        _this.videoWidth,
        _this.videoHeight
      )
      // 获取图片base64链接
      let image = this.cancas.toDataURL('image/png')
      this.fileData.set('imgData', image)
      _this.imgSrc = image //赋值并预览图片
    }


  }
}
</script>

<style lang='less' scoped>
/* you can make up upload button and sample style by using stylesheets */
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}

/deep/ .ant-upload.ant-upload-select-picture-card {
  width: 100%;
  height: 360px;
}
</style>